import axios from 'axios'
import nProgress from "nprogress"
import {
    getToken
} from "./token"

export const serverUrl = "http://localhost:1337"


const instance = axios.create({
    baseURL: serverUrl,//基本地址
    timeout: 5566//超时时间
})

instance.interceptors.request.use(config => {//请求拦截
    nProgress.start()//进度条开始
    config.headers.token = getToken()//在请求头中设置token
    return config
}, error => {
    return Promise.reject(error)
})

instance.interceptors.response.use(config => {//响应拦截
    nProgress.done()//进度条结束
    return config.data
    
}, (error) => {
    if(error.response.status===401){
        return {code:-1,tit:"未登录",error}
    }
    
    nProgress.done() //进度条结束
    return Promise.reject(error)
})

export const get = (url, params) => instance.get(url, {
    params
});

export const post = (url, data) => instance.post(url, data);

export const put = (url, data) => instance.put(url, data);

export const del=(url)=>instance.delete(url)

export default instance